<?php

namespace App\Models;


use App\Http\Traits\GlobalConstant;

class Order extends Orm
{
    //
    protected $table = 'farm_order';
    protected $dateFormat = 'U';
    public $timestamps = true;

    protected $casts = [
        'goods_snap'  => 'json',
        'rebate_desc' => 'json'
    ];
    protected $appends = [
        'order_tip'
    ];

    public function getOrderTipAttribute()
    {
        $addressArray = explode('@', $this->attributes['address']);
        return [$addressArray[0] . ' ' . $addressArray[2], $addressArray[0] . ' ' . $addressArray[1], $addressArray[2]];
    }

    public function getCropsKindAttribute()
    {
        return GlobalConstant::getKindMap($this->attributes['crops_kind']);
    }

    public function userInfo()
    {
        return $this->belongsTo(Users::class, 'user_id', 'id');
    }

    public function getPayAtAttribute()
    {
        return $this->attributes['pay_at'] ? date('Y-m-d H:i:s', $this->attributes['pay_at']) : '';
    }

    public function getDeliveryAtAttribute()
    {
        if (!empty($this->attributes['delivery_at'])) {
            return date('Y-m-d H:i:s', $this->attributes['delivery_at']);
        }
        return '';
    }


    public function goodsInfo()
    {
        return $this->belongsTo(CropsCfg::class, 'crops_id');
    }

    public function refundInfo()
    {
        return $this->hasOne(RefundOrder::class, 'order_id');
    }
}
